from logger import logger
logger.debug("Loaded key event handler module")

import state
import cfg
import pygame

def handle(e):
	if e.type == pygame.KEYDOWN:
		if e.key in cfg.CAM_LEFT_KEYS:
			state.cam.move.x -= 1
			state.cam.move.keyboard_x = True
			logger.debug("Moving left")
		elif e.key in cfg.CAM_RIGHT_KEYS:
			state.cam.move.x += 1
			state.cam.move.keyboard_x = True
		elif e.key in cfg.CAM_UP_KEYS:
			state.cam.move.y -= 1
			state.cam.move.keyboard_y = True
		elif e.key in cfg.CAM_DOWN_KEYS:
			state.cam.move.y += 1
			state.cam.move.keyboard_y = True
		elif e.key == pygame.K_ESCAPE:
			state.quit = True
		else:
			logger.debug("Keydown event handled and not matched - keycode: " + str(e.key))
	elif e.type == pygame.KEYUP:
		if e.key in (pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN):
			if not state.cam.move.mouse:
				if e.key in (pygame.K_LEFT, pygame.K_RIGHT):
					state.cam.move.x = 0
					state.cam.move.keyboard_x = False
				elif e.key in (pygame.K_UP, pygame.K_DOWN):
					state.cam.move.y = 0
					state.cam.move.keyboard_y = False
